/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rmi;

import aso.upna.playlist.GenericList;
import aso.upna.playlist.PlayList;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author javier
 */

public class UserHand extends UnicastRemoteObject implements UserHandler {

    private String path_base;

    public UserHand(String path_base) throws RemoteException
    {
        super();
        this.path_base = new String(path_base);
    }

    public boolean nuevaPlaylist(String user, PlayList p) throws RemoteException {
        FileOutputStream fo = null;

        ObjectOutputStream os = null;
        try
        {
            File f = new File(path_base + user + "/Playlists/" + p.getName() + ".bin");
            if (f.exists())
            {
                f.createNewFile();
            }

            fo = new FileOutputStream(f);
            os = new ObjectOutputStream(fo);

            os.writeObject(p);
            return true;
        }
        catch (IOException ex)
        {
           return false;
        }
        finally
        {
            try
            {
                fo.close();
            }
            catch (IOException ex)
            {
                return false;
            }
            try
            {
                os.close();
            }
            catch (IOException ex)
            {
                return false;
            }
        }
    }

    public PlayList cargarPlaylist(String user, String p_name) throws RemoteException {

        FileInputStream fi = null;
        ObjectInputStream oi = null;

        File f = new File(path_base + user + "/Playlists/"+p_name+".bin");

        if(!f.exists())
            return null;
        try
        {
            fi = new FileInputStream(f);
            oi = new ObjectInputStream(fi);

            Object aux = oi.readObject();

            if(aux instanceof PlayList)
                return (PlayList)aux;

            return null;
        }
        catch (ClassNotFoundException ex)
        {
            return null;
        }
        catch (IOException ex)
        {
            return null;
        }      
    }

    public GenericList listarPlaylist(String user) throws RemoteException {
        GenericList list = new GenericList();
        int i;

        File f = new File(path_base+user+"/Playlists/");

        if(!f.exists())
            return null;

        String aux[] = f.list();

        for(i=0;i<aux.length;i++)
        {
            list.addElement(aux[i]);
        }
        
        return list;
    }

    public boolean borrarPlaylist(String user, String p_name) throws RemoteException {
        
        File f = new File(path_base + user + "/Playlists/"+p_name+".bin");

        if(!f.exists())
            return false;
        
        f.delete();
        return true;
    }

    public boolean checkLogin(String user, String pass) throws RemoteException {
        FileReader fr = null;
        
        try
        {
            File f = new File(path_base + user + "/pass.pwd");
            if (!f.exists())
            {
                return false;
            }
            fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            String linea = br.readLine();
            fr.close();

            if(linea.equals(pass))
                return true;

           return false;
        }
        catch (IOException ex)
        {
            try 
            {
                fr.close();
                ex.printStackTrace();
                return false;
            }
            catch (IOException ex1)
            {
                ex1.printStackTrace();
                return false;
            }
        }
    }

}
